import java.io.Serializable;

import javax.jms.JMSException;

import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Publisher implements Serializable{

	private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

	// private static String subject = "Topic1";

	TopicPublisher pub;


	TopicSession session;

	public TopicPublisher getPub() {
		return pub;
	}

	public TopicSession getSession() {
		return session;
	}

	public void setPub(TopicPublisher pub) {
		this.pub = pub;
	}

	

	public Publisher(String topicID, TopicConnection connection) {

		try {

			session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

			Topic topic = session.createTopic(topicID);

			pub = session.createPublisher(topic);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// public static void main(String[] args) throws JMSException {
	//
	// TopicConnectionFactory connectionFactory = new
	// ActiveMQConnectionFactory(url);
	//
	// final TopicConnection connection =
	// connectionFactory.createTopicConnection();
	// connection.start();
	//
	// final TopicSession session = connection.createTopicSession(false,
	// TopicSession.AUTO_ACKNOWLEDGE);
	//
	// Topic topic = session.createTopic(subject);
	//
	// final TopicPublisher publisher = session.createPublisher(topic);
	//
	// // ObjectMessage msg = session.createObjectMessage(new
	// ArrayList<Integer>());
	//
	// JFrame f = new JFrame();
	// JButton b = new JButton("send");
	// b.addActionListener(new ActionListener(){
	//
	// @Override
	// public void actionPerformed(ActionEvent arg0) {
	// TextMessage message;
	// try {
	// message = session.createTextMessage("Hallo!");
	// publisher.send(message);
	// System.out.println("Producer sent message: '" + message.getText() + "'");
	// } catch (JMSException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	//
	//
	// }
	//
	// });
	//
	// f.add(b);
	// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	// f.setSize(new Dimension(400, 400));
	// f.setVisible(true);
	//
	// Runtime.getRuntime().addShutdownHook(new Thread(){
	//
	// public void run(){
	// try {
	// connection.close();
	// } catch (JMSException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// });
	//
	//
	// }
}
